import { useUserStore } from "../stores/userStore";
import { useMenuStore } from "../stores/menuStore";
import { useTabStore } from "../stores/tabStore";
import Http from "../api/http";

export default {
  //退出登录
  loginOut() {
    useUserStore().clearToken();
    useMenuStore().clearMenu();
    useTabStore().clearAllTabs();
  },
  //获取用户信息
  async getInfo() {
    //获取缓存信息
    let resStroeData = await getStoreData();
    if (resStroeData.code == 200) {
      console.log('提示','执行缓存数据刷新一次');
      //把菜单存入缓存
      useMenuStore().setData(resStroeData.meunData);
      //把角色存入缓存
      useUserStore().setRole(resStroeData.roleData);
      //把权限存入缓存
      useUserStore().setAuthority(resStroeData.authorityData);
      //把用户信息存入缓存
      useUserStore().setUser(resStroeData.userData);
    }
    return resStroeData;
  },
};

const getStoreData = async (): Promise<resObj> => {
  let token = useUserStore().tokenGetter;

  let resultObj: resObj = {
    code: 200,
    msg: "",
    meunData: [],
    authorityData: [],
    roleData: [],
    userData: [],
  };
  //把菜单存入缓存
  await Http.get("/sys/menu").then((res) => {
    if (res.code == 200) {
      resultObj.meunData = res.data;
    }
    resultObj.code = res.code;
    resultObj.msg = res.msg;
  });
  //把角色存入缓存
  //把权限存入缓存
  await Http.get("/user/info", {}).then((res) => {
    if (res.code == 200) {
      resultObj.authorityData = res.data.permissions;
      resultObj.roleData = res.data.roles;
      resultObj.userData = res.data.user;
    }
    resultObj.code = res.code;
    resultObj.msg = res.msg;
  });

  return resultObj;
};
